/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faesa.acervo.business.ejb.msg;

import br.faesa.acervo.business.ejb.EmprestimoDAO;
import br.faesa.acervo.entity.Emprestimo;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 *
 * @author Eric Rosetti Lessa
 */
@MessageDriven(mappedName = "jms/ReservaQueue",
        activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
public class CancelamentoReservaMessageBean implements MessageListener {
    
    @EJB
    private EmprestimoDAO emprestimoDAO;
    
    public CancelamentoReservaMessageBean() {
    }
    
    @Override
    public void onMessage(Message message) {
       
       try{ 
        
           String idEmprestimo = message.getStringProperty("idEmprestimo");
           
           Emprestimo emp = emprestimoDAO.find(Long.valueOf(idEmprestimo));
           
           if(emp != null){
                emprestimoDAO.remove(emp);
           }
       
       } catch (JMSException e) {
            throw new RuntimeException(e);
       }
       
    }
    
}
